//======================================================================================
//  Notes:Use this command to anonymously check out the latest project source code:
//  Non-members may check out a read-only working copy anonymously over HTTP.
//  http://YHQCodingWsp.googlecode.com/svn/trunk/
//
//  Author:		Kevin Yang
//  Email:		Bigearrabbit@qq.com
//
//  DATE:		2013-4-16
//
//  File Name:	YHQFolderCAAOperate.cpp
//
//  Notes:
//
//======================================================================================
#include "YHQFolderCAAOperate.h"

//YHQCodingCore Library
#include "YHQFile.h"

//Qt Library
#include <QDir>
#include <QStringList>
#include <QProcess>


//头文件包含
#include <QFileInfo>
#include <QString>
#include <QList>
#include <QFile>


#include <iostream>

//LocalOutPutMessage
#define LocalOutPutMessage(iString) \
    if(m_pOutsideTextEdit)m_pOutsideTextEdit->append(iString)

//----------------------------------------------------
YHQFolderCAAOperate::YHQFolderCAAOperate()
    : m_pOutsideTextEdit(NULL)
    , m_ListOfDelete()
    , m_ListOfIgnore()
{
}
//----------------------------------------------------
bool YHQFolderCAAOperate::DeleteNoUseContents(const QString & iFolderDir)
{
    const int countOfListDelete = m_ListOfDelete.size();//size
    const int countOfListIgnore = m_ListOfIgnore.size();//size
    if( 0 == countOfListDelete)//check list
    {
        LocalOutPutMessage("Error: count Of List Delete is 0");
        return false;
    }

    QDir dir(iFolderDir);//instance a dir
    if (!dir.exists())  return false;// not exist
    QStringList list = dir.entryList(QDir::AllDirs|
                                     QDir::NoDotAndDotDot|
                                     QDir::System|
                                     QDir::Hidden);//List of dirs

    bool isFindInList = false;//not find

    //loop sub folder
    const int size = list.size();//sub size
    for (int i=0;i<size;i++)
    {
        QString fileInfo = list.at(i);
        QString folderSub = iFolderDir;
        folderSub.append("/");
        folderSub.append(fileInfo);
        fileInfo = fileInfo.toLower();// to lower

        isFindInList = false;//not find
        //check ignore list first
        for(int j= 0;j<countOfListIgnore;j++)
        {
            //if in list
            if(fileInfo == m_ListOfIgnore[j])
            {
                isFindInList = true;//find
                break;
            }//if

        }//for j
        if (isFindInList) continue;//next i

        //check the foler name list for delete
        for(int j= 0;j<countOfListDelete;j++)
        {
            //if in list
            if(fileInfo == m_ListOfDelete[j])
            {
                isFindInList = true;//find it
                //delete folder
                YHQFile::DeleteFolderContents(folderSub);//Delete Contents
                dir.rmdir(fileInfo);
                LocalOutPutMessage("Delete folder:" + folderSub );
                break;//goto next i

            }//if
        }//for j

        //not in list
        if(!isFindInList) DeleteNoUseContents(folderSub);//call self again for sub folder
    }//for i
    return true;
}
//----------------------------------------------------
bool YHQFolderCAAOperate::DeleteFolderOfObjects(const QString & iFolderDir)
{
    QDir dir(iFolderDir);//instance a dir
    if (!dir.exists())//check exist
    {
        LocalOutPutMessage("folder is not exist:" + iFolderDir );
        return false;// not exist
    }
    QStringList list = dir.entryList(QDir::AllDirs|
                                     QDir::NoDotAndDotDot|
                                     QDir::System|
                                     QDir::Hidden);//List of dirs

    //loop sub folder
    const int size = list.size();//sub size
    for (int i=0;i<size;i++)
    {
        QString fileInfo = list.at(i);
        QString folderSub = iFolderDir;
        folderSub.append("/");
        folderSub.append(fileInfo);
        fileInfo = fileInfo.toLower();// to lower

        //if in list
        if(fileInfo == "objects")
        {
            //delete folder
            YHQFile::DeleteFolderContents(folderSub);//Delete Contents
            dir.rmdir(fileInfo);
            LocalOutPutMessage("Delete folder:" + folderSub );
        }//if
        else
        {
            //not in list
            DeleteFolderOfObjects(folderSub);//call self again for sub folder
        }
    }//for i
    return true;
}
//---------------------------------------------------------------------
